<?php
namespace app\admin\controller;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Good
{
    public function goodList()
    {
        $keywords=Request::post('keywords');
        $post = Request::post();
        $where=[];
        if(isset($keywords) && $keywords <> '')                     $where[] = ['g.name','like',"%{$keywords}%"];
        if (isset($post['is_down']) && count($post['is_down'])==1)  $where[] = ['is_down','in',$post['is_down']];
        if (isset($post['is_show']) && $post['is_show']<>'')        $where[] = ['is_show','=',$post['is_show']];
        if (isset($post['is_hot']) && $post['is_hot']<>'')           $where[] = ['is_hot','=',$post['is_hot']];
        if(isset($post['k_time']) && !empty($post['k_time']))        $where[] = ['g.addtime','> TIME',$post['k_time']];
        if(isset($post['j_time']) && !empty($post['j_time']))        $where[] = ['g.addtime','< TIME',$post['j_time']];
        if(isset($post['price1']) && $post['price1']<>'') $where[] = ['price','>=',$post['price1']];
        if(isset($post['price2']) && !empty($post['price2'])) $where[] = ['price','<=',$post['price2']];
        if(isset($post['cid']) && !empty($post['cid'])) $where[] = ['cid','=',$post['cid']];
        View::assign($post);//View::assign('keyword',$keywords);
        $goods=Db::name('goods')
            ->alias('g')
            ->field('g.*,c.name cname')
            ->join('category c','g.cid=c.id')
            ->where($where)
            ->select();
        View::assign('goods',$goods);
        //方案一
//        $cate=Db::name('category')->where('parentid','0')->select()->toArray();
//        foreach ($cate as &$v){
//            $v['son'] = Db::name('category')->where('parentid',$v['id'])->select()->toArray();
//        }
        //方案二
        //使用缓存
        if (Cache::has('cate')){//判断缓存变量是否存在
            $cateAll = Cache::get('cate');//获取缓存数据
        }else{
            $cateAll=Db::name('category')->select()->toArray();
            Cache::set('cate',$cateAll,100);//设置缓存变量
        }
        //2.1
//        $cate = [];
//        //循环所有分类 $v为每条分类
//        foreach ($cateAll as $v){
//            //循环分类中parentid字段等于0 进入判断结果 都是一级分类
//            if($v['parentid'] == 0){
//                //将一级分类赋值到空数组中 索引为自己的ID
//                $cate[$v['id']] = $v;
//                //循环所有分类
//                foreach ($cateAll as $vv){
//                    //如果分类的parentid等于上一级分类(一级分类) ID 进入判断里的是二级分类
//                    if($vv['parentid'] == $v['id']){
//                        //将二级分类赋值到数组 根据索引 ($v.id) 将一级和二级绑定到一起
//                        $cate[$v['id']]['son'][$vv['id']] = $vv;
//                    }
//                }
//            }
//        }

        //2.2
        $cate=$this->getCate($cateAll,0);
        View::assign('cate',$cate);
        return View::fetch();
    }

    public function getCate($cateAll,$parentid)
    {
        $cate = [];
        foreach ($cateAll as $v){
            if($v['parentid'] == $parentid){
                $cate[$v['id']] = $v;
                $cate[$v['id']]['son']=$this->getCate($cateAll,$v['id']);
            }
        }
        return $cate;
    }

    public function goodAdd()
    {
        return View::fetch();
    }
}